<template>
  <div class="app-container">
    <search-bar class="search">
      <template slot="peopleSearch">
        <el-form ref="form" :model="searchType" label-width="80px">
          <el-row type="flex">
            <el-form-item label="商品类型:">
              <el-col :span="24">
                <el-input
                  v-model="searchType.className"
                  placeholder="请输入"
                  class="input"
                  @input="focusInp"
                  @blur="blurInp"
                >
                  <i slot="suffix" :class="{'l-input__icon el-icon-circle-close':inpFocus}" @click="searchType.className=''" />
                </el-input>
              </el-col>
            </el-form-item>
            <!-- 搜索按钮 -->
            <el-form-item>
              <el-col class="searchButton">
                <el-button type="primary" @click="search(searchType.className)">
                  <i class="el-icon-search" />搜索</el-button>
              </el-col>
            </el-form-item>
          </el-row>
        </el-form>
      </template>
    </search-bar>
    <el-card>
      <!-- 新增按钮 -->
      <el-row class="addButton">
        <el-col>
          <el-button type="warning" @click="showDialog = true">
            <i class="el-icon-circle-plus-outline" /> 新增</el-button>
        </el-col>
      </el-row>
      <!-- 表格表单 -->
      <el-table
        :data="goodsType"
        style="width: 100% "
        stripe
        :row-style="{height: '10'}"
        :cell-style="{padding: '0'}"
        :header-cell-style="{color:'#000',background:'#f3f6fb'}"
      >
        <el-table-column
          prop="classId"
          label="序号"
          width="150"
          align="center"
        >
          <template v-slot="{$index}">{{ $index+1 }}</template>
        </el-table-column>
        <el-table-column
          prop="className"
          label="商品类型名称"
          width="700"
        />
        <el-table-column
          label="操作"
          align="center"
        >
          <template v-slot="{row}">
            <el-button type="text" @click="editGoodType(row.classId,row.className)">修改</el-button>
            <el-button type="text" style="color:hotpink" @click="delGoodsType(row.classId)">删除</el-button>
          </template>
        </el-table-column>

      </el-table>
      <el-row type="flex" justify="center" align="middle" style="height: 60px">
        <el-pagination
          layout="prev, pager, next"
          :total="+total"
          :current-page="searchConfig.pageIndex"
          :page-size="searchConfig.pageSize"
          background
          @current-change="currentChange"
        />
      </el-row>
      <!-- 表格表单 -->
    </el-card>
    <el-dialog :visible="showDialog" :title="goodsName.classId?'编辑商品类型':'新增商品类型'" @close="closeDialog">
      <el-form ref="dialogForm" :rules="rules" :model="goodsName" label-width="120px">
        <el-form-item label="商品类型名称:" prop="className">
          <el-input
            v-model="goodsName.className"
            size="mini"
            inline-message
            label-width
            placeholder="请输入"
          />
        </el-form-item>
        <el-row class="dialogButton">
          <el-button round type="primary" @click="submit">确认</el-button>
          <el-button round @click="closeDialog">取消</el-button>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { addGoodsTypeAPI, delGoodsTypeAPI, editGoodsTypeAPI, getGoodsTypeAPI } from '@/api/goods'
export default {
  data() {
    return {
      // 查找类型
      searchType: {},
      // 根据商品类型名添加商品类型
      goodsName: {
        classId: '',
        className: ''
      },
      // 商品类型
      goodsType: [],
      // 渲染页
      searchConfig: {
        pageIndex: 1,
        pageSize: 10
      },
      total: 0,
      showDialog: false,
      rules: {
        className: [{ required: true, message: '商品类型不能为空', trigger: 'blur' }]
      },
      inpFocus: false
    }
  },
  created() {
    this.getGoodType()
  },
  methods: {
    // 获取商品类型列表
    async getGoodType() {
      const res = await getGoodsTypeAPI(this.searchConfig)
      this.goodsType = res.data.currentPageRecords
      this.total = res.data.totalCount
    },
    // 查询
    async search() {
      if (this.searchType.className) {
        const res = await getGoodsTypeAPI({ className: this.searchType.className })
        this.$message.success('恭喜，查询成功！！')
        this.goodsType = res.data.currentPageRecords
      } else {
        this.getGoodType()
      }
    },
    // 关闭dialog
    closeDialog() {
      this.showDialog = false
      this.$refs.dialogForm.resetFields()
      this.goodsName = {}
    },
    // 点击dialog的确定按钮
    async submit() {
      if (this.goodsId) {
        const res = await editGoodsTypeAPI(this.goodsName.classId, this.goodsName.className)
        console.log(res)
      } else {
        await addGoodsTypeAPI(this.goodsName)
      }
      this.$message.success('恭喜，添加成功！！')
      await this.getGoodType()
      this.closeDialog()
    },
    // 更换页面
    currentChange(newPage) {
      this.searchConfig.pageIndex = newPage
      this.getGoodType()
    },
    // 点击编辑，数据回显，打开弹框
    async editGoodType(classId, className) {
      this.goodsName.className = className
      this.goodsName.classId = classId
      this.showDialog = true
    },
    // 删除
    async delGoodsType(id) {
      await delGoodsTypeAPI(id)
      this.$message.success('恭喜，删除成功！！')
      await this.getGoodType()
    },
    // 失去焦点
    blurInp() {
      setTimeout(() => {
        this.inpFocus = false
      }, 200)
    },
    focusInp() {
      if (this.searchType) {
        this.inpFocus = true
      }
    }

  }
}
</script>

<style lang="scss" scoped>
.search{
width: 100%;
.searchButton{
  margin-left:-70px;
}
}
.addButton{
  margin-bottom: 20px;
}
.dialogButton{
  width: 100%;
  margin: 0 210px;
}

</style>

